/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.diy.sigmund.diyorder.common.middleware.mq.rocketmq.spring.one;

import com.diy.sigmund.diycommon.test.entity.MqTestVO;
import com.diy.sigmund.diycommon.util.json.JacksonUtil;
import java.util.concurrent.TimeUnit;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQReplyListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * The consumer that replying generic type
 * <pre code="class">
 * 回复的消费者要与它配合使用
 * ProductWithPayload<String> sendResult = rocketMQTemplate.sendAndReceive("FAST_PROCESSING_TOPIC:*",
 *                             new String(msg.getBody()),
 *                             new TypeReference<ProductWithPayload<String>>() {
 *                             }.getType(),
 *                             30000,
 *                             0);
 *     </pre>
 */
@Service
@RocketMQMessageListener(
        topic = "FAST_PROCESSING_TOPIC",
        consumerGroup = "CID_FAST_PROCESSING")
public class FastProcessingReplyConsumer implements RocketMQReplyListener<String, ProductWithPayload<MqTestVO>> {

    private static final Logger log = LoggerFactory.getLogger(FastProcessingReplyConsumer.class);

    @Override
    public ProductWithPayload<MqTestVO> onMessage(String message) {
        try {
            TimeUnit.MILLISECONDS.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            // Thread.currentThread().interrupt();
        }
        log.info("------- FastProcessingReplyConsumer received: {}", message);
        MqTestVO testVO = JacksonUtil.toObject(message, MqTestVO.class);
        log.info("Reply-积压消息快速消费结束");
        return new ProductWithPayload<>("replyProductName", testVO);
    }
}
